import { ApiProxy } from 'ApiProxy'

type APIQueryArgCollection = ApiTypes.APIQueryArgCollection
type APIBodyArgCollection = ApiTypes.APIBodyArgCollection
type ApiBaseResult<T extends object> = ApiTypes.ApiBaseResult<T>

export namespace api {
  export const AUTH_ERROR_CODES = ApiProxy.AUTH_ERROR_CODES

  function get<T extends ApiBaseResult<any> = any>(path: string, urlArgs?: APIQueryArgCollection): ApiProxy<T> {
    return new ApiProxy<T>('GET', path, urlArgs)
  }

  function post<T extends ApiBaseResult<any> = any>(path: string, urlArgs?: APIQueryArgCollection, bodyArgs?: APIBodyArgCollection): ApiProxy<T> {
    return new ApiProxy<T>('POST', path, urlArgs, bodyArgs)
  }

  // ================
  // api declarations
  // ===============

  export function testPost() {
    return post<ApiBaseResult<{}>>('/test', {}, {})
  }

  export function testGet() {
    return get<ApiBaseResult<{}>>('/test', {})
  }

  export function wxappAuth(
    code: string, iv: string, encryptedData: string
  ) {
    return post<ApiBaseResult<{ user: Models.User }>>('/auth/wxapp_login', {}, { code, iv, encryptedData })
  }

}

export default api
